package com.example.skinhelper;

import android.app.Activity;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class SkinViewManager {

    private HashMap<Activity, List<SkinView>> map=new HashMap<>();


    public void addSkinView(Activity activity,SkinView skinView){
        List<SkinView> skinViews;
        if (map.containsKey(activity)){
            skinViews = map.get(activity);
        }else {
            skinViews = new ArrayList<>();
            map.put(activity,skinViews);
        }
        skinViews.add(skinView);
    }


    public void skinActivity(Activity activity){
        if (map.containsKey(activity)){
            List<SkinView> skinViews = map.get(activity);
            for (SkinView skinView : skinViews) {
                if (!onStartSkinView(skinView)){
                    skinViews.remove(skinView);
                }
            }
        }
    }

    public void skinAllActivity(){
        for (Map.Entry<Activity, List<SkinView>> activityListEntry : map.entrySet()) {
            for (SkinView skinView : activityListEntry.getValue()) {
               if (!onStartSkinView(skinView)){
                   activityListEntry.getValue().remove(skinView);
               }

            }
        }
    }


    public boolean onStartSkinView(SkinView skinView){
        View targetView = skinView.getTargetView();
        if (targetView==null){
            return false;
        }
        Map<String, String> attrNV = skinView.getAttrNV();
        for (Map.Entry<String, String> stringStringEntry : attrNV.entrySet()) {

            List<ViewSetting> machViewSettings = SkinIns.getInstance().getViewSettings(stringStringEntry.getKey());
            if (machViewSettings.size()==0){ continue;}
            for (ViewSetting machViewSetting:machViewSettings
            ) {
                machViewSetting.setSkin(targetView,stringStringEntry.getKey(),stringStringEntry.getValue(),SkinIns.getInstance().getSkinResources());
            }
        }
        return true;
    }

    public void removeActivity(Activity activity){
        map.remove(activity);
    }

}
